﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tetris_dif
{
    class Figure
    {
        public Node[] figure = new Node[4];
        public FigureType figureType;
        public FigurePosition figurePosition = FigurePosition.First;
        protected Random rndType = new Random();

        public Figure()
        {
            ChooseFigure(rndType.Next(1, 8));
            for (int i = 0; i <= 4; i++)
            {
                figure[i] = new Node(ChooseColorForFigure());
            }
        }

        private FigureType ChooseFigure(int figureNumber)
        {
            if (figureNumber == 1)
                figureType = FigureType.Line;
            if (figureNumber == 2)
                figureType = FigureType.Square;
            if (figureNumber == 3)
                figureType = FigureType.ZLeft;
            if (figureNumber == 4)
                figureType = FigureType.ZRight;
            if (figureNumber == 5)
                figureType = FigureType.Piramyd;
            if (figureNumber == 6)
                figureType = FigureType.LLeft;
            if (figureNumber == 7)
                figureType = FigureType.LRight;
            return figureType;
        }

        public ConsoleColor ChooseColorForFigure()
        {
            if (figureType == FigureType.Line)
                return ConsoleColor.Blue;
            if (figureType == FigureType.LLeft)
                return ConsoleColor.DarkMagenta;
            if (figureType == FigureType.LRight)
                return ConsoleColor.Yellow;
            if (figureType == FigureType.Piramyd)
                return ConsoleColor.Green;
            if (figureType == FigureType.ZLeft)
                return ConsoleColor.Cyan;
            if (figureType == FigureType.ZRight)
                return ConsoleColor.DarkCyan;
            return ConsoleColor.Red;
        }

        public void MoveDown()
        {
            for (int i = 0; i <= 4; i++)
            {
                figure[i].Y++;
            }
        }

        public void MoveLeft()
        {
            for (int i = 0; i <= 4; i++)
            {
                figure[i].X--;
            }
        }

        public void MoveRight()
        {
            for (int i = 0; i <= 4; i++)
            {
                figure[i].X++;
            }
        }
    }

    class Line: Figure
    {

        public Line()
        {
            for (int i = 0; i <= 4; i++)
            {
                figure[i] = new Node(ConsoleColor.Blue);
            }
        }

       public void Rotate()
        {
            if (figurePosition == FigurePosition.First)
            {
                figure[1].X++;
                figure[1].Y++;
                figure[3].X--;
                figure[3].Y--;
                figure[4].Y++;
            }
        }

    }

    class Figura
    {
        public bool[,] matrix = new bool[4, 4]; //матрица контейнер для размещения фигур
        public FigureType type; //тип фигуры
        public int position; //положение фигуры


        //стирание фигуры
        public void Clear(bool[,] m)
        {
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                    m[i, j] = false;
        }

        //создание фигуры
        public void Create(FigureType figtype)
        {
            Clear(matrix); //стираем фигуру
            this.type = figtype;
            this.position = 1;
            switch (figtype) //анализируем переданную в функцию фигуру
            {
                case FigureType.Line: //линия
                    {
                        for (int i = 0; i < 4; i++)
                            matrix[0, i] = true;
                        break;
                    }

                case FigureType.Square: //квадрат
                    {
                        for (int i = 0; i < 2; i++)
                            for (int j = 0; j < 2; j++)
                                matrix[i, j] = true;
                        break;
                    }

                case FigureType.LLeft:
                    {
                        for (int i = 0; i < 3; i++)
                            matrix[0, i] = true;
                        matrix[1, 2] = true;
                        break;
                    }

                case FigureType.LRight:
                    {
                        for (int i = 0; i < 3; i++)
                            matrix[0, i] = true;
                        matrix[1, 0] = true;
                        break;
                    }

                case FigureType.Piramyd:
                    {
                        for (int i = 0; i < 3; i++)
                            matrix[1, i] = true;
                        matrix[0, 1] = true;
                        break;
                    }

                case FigureType.ZLeft:
                    {
                        matrix[0, 0] = true; matrix[1, 0] = true;
                        matrix[1, 1] = true; matrix[2, 1] = true;
                        break;
                    }

                case FigureType.ZRight:
                    {
                        matrix[0, 1] = true; matrix[1, 0] = true;
                        matrix[1, 1] = true; matrix[2, 0] = true;
                        break;
                    }
            }

        }

        //вращение фигуры
        public void Rotate()
        {
            if (this.position == 4) this.position = 1; //в начальном положении
            this.position++; //меняем положение

            switch (type)
            {
                case FigureType.Line: //если линия
                    {
                        int k;
                        if (matrix[0, 0] == true)
                        {
                            Clear(matrix);
                            for (k = 0; k < 4; k++)
                                matrix[k, 1] = true;
                        }
                        else
                        {
                            Clear(matrix);
                            for (k = 0; k < 4; k++)
                                matrix[0, k] = true;
                        }
                        break;
                    }

                case FigureType.Square: //если квадрат
                    {
                        return;
                    }

                default: //остальные виды фигур (так как они зеркально похожи)
                    {
                        bool[,] tempFig = new bool[4, 4];
                        Clear(tempFig);

                        for (int j = 3 - 1, c = 0; j >= 0; j--, c++)
                            for (int i = 0; i < 3; i++)
                                tempFig[c, i] = matrix[i, j];

                        Clear(matrix);

                        for (int f = 0; f < 3; f++)
                            for (int d = 0; d < 3; d++)
                                matrix[f, d] = tempFig[f, d];
                        break;
                    }
            }

        }


    }

    public enum FigureType
    {
        Line,
        Square,
        ZRight,
        ZLeft,
        LRight,
        LLeft,
        Piramyd
    }

    public enum FigurePosition
    {
        First,
        Second,
        Third,
        Fourth
    }
}
